//index.js
//获取应用实例
const app = getApp()

Page({

  data: {
    uid:'',
    userInfo: {}
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  
  onLoad: function (options) {


    this.setData({
      uid: options.phone
    }) 

    this.ctx = wx.createCameraContext()

    this.setData({
      firHorse: true,
      secHorse: false,
      thrHorse: false,
      forHorse: false
    })
  },

  // userNameInput: function (e) {

  //     this.setData({
  //       uid: e.detail.value
  //     })
  // },

  takePhoto() {

    wx.showLoading({
      title: '图片上传中',
    }),

    this.ctx.takePhoto({
      quality: 'high',
      success: (res) => {
        console.log('=====res======:', res.tempImagePath)
        this.setData({
          src: res.tempImagePath
        })

        var tempV = this.data.uid;
        wx.setStorageSync("phone", tempV);
        wx.setStorageSync('step', 1);

        wx.uploadFile({
          url: 'https://aqhy.citydo.com.cn/safe/mini/miniProgramFaceRegister',
          // url:'http://172.16.16.110:8180/safe/mini/miniProgramFaceRegister',
          filePath: res.tempImagePath,
          method: 'POST',
          name: "file",
          formData: {
            phone: this.data.uid
          },
          success: function (res) {
            wx.hideLoading();

            var data = res.data
            res = JSON.parse(res.data)
            console.info(res.msg)

            if(res.code == 0 ){

              wx.showModal({
                title: '提示',
                content: res.msg,
                showCancel: false,//是否显示取消按钮

                success: function (res) {
                  // wx.setStorageSync("phone", res.data)
                  if (res.cancel) {
                    //点击取消,默认隐藏弹框
                  } else {
                    //点击确定
                    wx.redirectTo({
                      url: '../registervoice/registervoice?step=' + '1'
                    })
                  }
                },
                fail: function (res) { },//接口调用失败的回调函数
                complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会

              })

            }else {

              if (res.code == 20005) {

                wx.redirectTo({
                  url: '../registervoice/registervoice?step=' + '1'
                })

              }else {

                wx.showModal({
                  title: '提示',
                  content: res.msg,
                  success: function (res) {
                    wx.redirectTo({
                      url: '../index/index'
                    })
                  }
                })
              }
            }

          },
          fail: function () {
            wx.showModal({
              title: '提示',
              content: '网络失败'
            })
          }
          // ,
          // complete: () => {
          //   wx.showModal({
          //     title: '提示',
          //     content: '照片上传完成'
          //   })
          // }
        })
      }
    })
  }
})
